package com.newlink.building.secret.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.newlink.building.common_base.constant.Base_DoorKeyRepeatType
import com.newlink.building.common_base.model.bean.SecretKeyData
import com.newlink.building.secret.R
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by xiaodong on 2022/8/30.
 */
class Module_Secret_SecretListAdapter :
    BaseQuickAdapter<SecretKeyData, BaseViewHolder>(R.layout.item_secretkey), LoadMoreModule {
    override fun convert(holder: BaseViewHolder, item: SecretKeyData) {
        if (item == null) {
            return
        }
        holder.setText(R.id.tv_secret_name, item.name)
        holder.setText(R.id.tv_repeat, formatRepeatName(item.repeatType))

        if (item.repeatType == Base_DoorKeyRepeatType.NoRepeat.type) {
            holder.setText(
                R.id.tv_start_time,
                context.resources.getString(R.string.secretkey_detail_start_time) + formatPeriod(
                    item.period.orEmpty(),
                    true
                )
            )
            holder.setText(
                R.id.tv_end_time,
                context.resources.getString(R.string.secretkey_detail_end_time) + formatPeriod(
                    item.period.orEmpty(),
                    false
                )
            )
            holder.setGone(R.id.tv_effctive_time, true)
        } else {

            holder.setText(
                R.id.tv_start_time,
                context.resources.getString(R.string.secretkey_detail_start_date) + formatDateFromPeriod(
                    formatPeriod(item.period.orEmpty(), true)
                )
            )
            holder.setText(
                R.id.tv_end_time,
                context.resources.getString(R.string.secretkey_detail_end_date) + formatDateFromPeriod(
                    formatPeriod(item.period.orEmpty(), false)
                )
            )

            holder.setText(
                R.id.tv_effctive_time,
                context.resources.getString(R.string.secretkey_detail_effective_time) + formatTimeFromPeriod(
                    formatPeriod(item.period.orEmpty(), true)
                ) + " - " + formatTimeFromPeriod(formatPeriod(item.period.orEmpty(), false))
            )
            holder.setGone(R.id.tv_effctive_time, false)
        }

        if (item.devices != null && item.devices.size > 0) {
            holder.setImageResource(
                R.id.image_secret,
                if ((item.devices[0].openNum > 0 || item.devices[0].openNum == -1) && isExpire(item.period.orEmpty())) R.drawable.ic_secret_green else R.drawable.ic_secret_grey
            )
        } else {
            holder.setImageResource(R.id.image_secret, R.drawable.ic_secret_grey)
        }
    }

    private fun isExpire(period: String): Boolean {
        var mIsExpire = false
        try {
            if (period.contains("-")) {
                val split = period.split(" - ")
                if (split.size > 1) {
                    val endTime = split[1]
                    val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                    val date: Date = dateFormat.parse(endTime)
                    val timestamp: Long = date.getTime()
                    mIsExpire = timestamp > System.currentTimeMillis()
                }
            }
        } catch (e: Exception) {
            mIsExpire = false
        }
        return mIsExpire
    }

    private fun formatRepeatName(type: Int): String {
        val periodArray =
            context.resources.getStringArray(R.array.secretkey_array_repetition_period)
        var name = periodArray[0]
        //type == index + 1
        if (type > 0 && type < periodArray.size + 1) {
            name = periodArray[type - 1]
        }

        return context.getString(R.string.secretkey_list_repeat, name)
    }

    private fun formatPeriod(period: String, isStart: Boolean): String {
        if (period.contains("-")) {
            val split = period.split(" - ")
            if (split.size > 1) {
                val startTime = split[0]
                val endTime = split[1]
                return if (isStart) startTime else endTime
            }
        }
        return period
    }

    private fun formatDateFromPeriod(time: String): String {
        var date = ""
        val split = time.split("\\s+".toRegex())
        if (split.isNotEmpty()) {
            date = split[0]
        }
        return date
    }

    private fun formatTimeFromPeriod(time: String): String {
        var timeFormat = ""
        val split = time.split("\\s+".toRegex())
        if (split.size > 1) {
            timeFormat = split[1]
        }
        return timeFormat
    }
}
